package com.qcby.service.impl;

import com.qcby.dao.HallSeatArrayMapper;
import com.qcby.model.HallSeatArray;
import com.qcby.model.dto.HallSeatArrayQueryDto;
import com.qcby.service.HallSeatArrayService;
import com.qcby.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HallSeatArrayServiceImpl implements HallSeatArrayService {
    @Autowired
    private HallSeatArrayMapper hallSeatArrayMapper;

    @Override
    public Page<HallSeatArray> selectConditionPage(HallSeatArrayQueryDto queryDto) {
        Integer pageNo = queryDto.getPageNo();
        Integer pageSize = queryDto.getPageSize();
        Integer hId = queryDto.getHId();
        Integer hsId = queryDto.getHsId();
        Integer rowIndex = queryDto.getRowIndex();
        Integer lineIndex = queryDto.getLineIndex();
        Page<HallSeatArray> page = new Page<>();
        page.setPageNo(pageNo);

        Integer startNum = null;
        if (pageSize != null) {
            startNum = (pageNo - 1) * pageSize;
            page.setStartNum(startNum);
            page.setPageSize(pageSize);
        }

        List<HallSeatArray> hallSeatArrayList = hallSeatArrayMapper.selectCondition(hId, hsId, rowIndex, lineIndex,
                startNum, pageSize);
        Integer count = hallSeatArrayMapper.selectConditionCount(hId, hsId, rowIndex, lineIndex);


        //把总记录数设置给page对象
        page.setTotalCount(count);
        page.setList(hallSeatArrayList);
        if (pageNo != null && pageSize != null) {
            page.setTotalPage(count % pageSize == 0 ? count / pageSize : (count / pageSize) + 1);
        }
        return page;
    }

    @Override
    public void updateValue(Integer rowIndex, Integer lineIndex, Integer hid) {
        hallSeatArrayMapper.updateValue(rowIndex, lineIndex, hid);
    }
}
